package com.hxy.pet.controller;

import com.hxy.basic.query.util.AjaxResult;
import com.hxy.basic.query.util.PageList;
import com.hxy.pet.domain.Pet;
import com.hxy.pet.domain.PetDetail;
import com.hxy.pet.query.PetQuery;
import com.hxy.pet.service.IPetDetailService;
import com.hxy.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 宠物Controller
 */
@Api(tags = "宠物接口",description = "宠物接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private IPetDetailService petDetailService;

    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/pet/1
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/{id}")//  pet/1    或 /pet/2
    public Pet findById(@PathVariable("id")Long id){
        return petService.loadById(id);
    }


    //获取所有对象：get
    //Get http://localhost:8080/pet
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<Pet> findAll(){
        return petService.findAll();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/pet + json参数petQuery
    @ApiOperation(value = "分页查询宠物信息",notes = "需要传递当前页和每页显示的条数")
    @PostMapping
    public PageList<Pet> findPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    /**
     * 删除接口
     * @param id - 传递需要删除的id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口",notes = "需要传递id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败，" + e.getMessage());
        }
    }
    /**
     * 添加或修改接口
     * @param pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口",notes = "需要传递对象")
    public AjaxResult saveOrUpdate(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){//添加操作
                petService.save(pet);
            }else{
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败，" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids - 需要传递删除对象的id
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传递删除对象的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            petService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("批量删除失败，" + e.getMessage());
        }
    }


    /**
     * 根据宠物id查询详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public PetDetail getDetail(@PathVariable("id") Long id){
        return petDetailService.getByPetId(id);
    }

    //宠物上架
    @PostMapping("/onsale")
    @ApiOperation(value = "宠物上架接口",notes = "传入批量需要上架的宠物的id组成的数组")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        }
    }

    //宠物下架
    @PostMapping("/offsale")
    @ApiOperation(value = "宠物下架接口",notes = "传入批量需要下架的宠物的id组成的数组")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 领养接口
     * @return
     */
    @PostMapping("/adopt")
    public AjaxResult adopt(@RequestBody Map<String,Long> map, HttpServletRequest request){
        try {
            petService.adopt(map.get("id"),request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("领养失败，" + e.getMessage());
        }
    }

    /**
     * 判断是否是店铺管理员
     * @param request
     * @return
     */
    @PostMapping("/isShopAdmin")
    public AjaxResult isShopAdmin(HttpServletRequest request){
        try {
            petService.isShopAdmin(request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false);
        }
    }
}
